package com.tilas.controller;


import com.tilas.pojo.Emp;
import com.tilas.pojo.EmpQueryParam;
import com.tilas.pojo.PageResult;
import com.tilas.pojo.Result;
import com.tilas.service.DepService;
import com.tilas.service.EmpSerivce;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;


@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpSerivce empSerivce;



    @GetMapping
    public Result page (EmpQueryParam empQueryParam) {
        log.info("分页查询：{}}", empQueryParam);
        PageResult<Emp> pageResult =empSerivce.page(empQueryParam);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody  Emp emp) {
        log.info("新增员工信息：{}",emp);
        empSerivce.save(emp);
        return Result.success(emp);
    }


    /**
     * 删除员工 - 数组
     * */
//    @DeleteMapping("/emps")
//    public Result delete(Integer[] ids){
//        log.info("删除员工:{}", Arrays.toString(ids));
//        return Result.success();


    /**
     * 删除员工 - list集合
     *
     * */

    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工:{}",ids);
        empSerivce.delete(ids);
        return Result.success();
    }


    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询的员工id:{}",id);
        Emp emp = empSerivce.findByid(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改的员工id:{}",emp);
        emp = empSerivce.update(emp);
        return Result.success(emp);
    }

    /**
     * 查询所有员工信息
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有员工信息");
        List<Emp> list = empSerivce.list();
        return Result.success(list);
    }

}
